/*
 * RtpSenderReceiver.h
 *
 *  Created on: Aug 24, 2011
 *      Author: user
 */


#include "talk/base/buffer.h"
#include "talk/base/event.h"
#include "talk/base/logging.h"
#include "talk/base/pathutils.h"
#include "talk/base/stream.h"
#include "talk/session/phone/rtpdump.h"
#include "talk/session/phone/rtputils.h"
#include "talk/base/criticalsection.h"
#include "talk/session/phone/mediachannel.h"
#include "talk/session/phone/mediaengine.h"

#include "control.h"
#include "v4l2uvc.h"
//#include "utils.h"


#ifndef RTPSENDERRECEIVER_H_
#define RTPSENDERRECEIVER_H_


namespace cricket {
///////////////////////////////////////////////////////////////////////////
// Definition of RtpSenderReceiver.
///////////////////////////////////////////////////////////////////////////
class RtpSenderReceiver
    : public talk_base::Thread, public talk_base::MessageHandler {
 public:
    	RtpSenderReceiver(MediaChannel* channel);
    	enum {
    	  MSG_GET_FRAME = 1,
    	  MSG_SEND_FRAME,
    	  MSG_FREE_BUFFER,
    	  MSG_SI_DESTROY,
    	};

  // Called by media channel. Context: media channel thread.
  bool SetSend(bool send);
  void OnPacketReceived(talk_base::Buffer* packet);

  // Override virtual method of parent MessageHandler. Context: Worker Thread.
  virtual void OnMessage(talk_base::Message* pmsg);


  struct control_data cd, *cdata;
  int stop, g_size;
  unsigned char *g_buf;
  talk_base::Buffer *packet_;

 private:
  // Read the next RTP dump packet, whose RTP SSRC is the same as first_ssrc_.
  // Return true if successful.
  bool ReadNextPacket(RtpDumpPacket* packet);
  // Send a RTP packet to the network. The input parameter data points to the
  // start of the RTP packet and len is the packet size. Return true if the sent
  // size is equal to len.
  bool SendRtpPacket(const void* data, size_t len);

  MediaChannel* media_channel_;
  talk_base::scoped_ptr<talk_base::StreamInterface> input_stream_;
  talk_base::scoped_ptr<talk_base::StreamInterface> output_stream_;
  talk_base::scoped_ptr<RtpDumpLoopReader> rtp_dump_reader_;
  talk_base::scoped_ptr<RtpDumpWriter> rtp_dump_writer_;
  // RTP dump packet read from the input stream.
  RtpDumpPacket rtp_dump_packet_;
  uint32 start_send_time_;

  bool sending_;
  char *boundry_;
  bool first_packet_;
  uint32 first_ssrc_;

  mutable talk_base::CriticalSection cs_;
  DISALLOW_COPY_AND_ASSIGN(RtpSenderReceiver);
};
}

#endif /* RTPSENDERRECEIVER_H_ */
